package com.wshl.aijiuunion.model;

import android.app.Activity;
import android.content.Context;
import android.os.Message;
import android.text.TextUtils;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okgo.request.base.Request;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.NetUrl;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.callback.RequestCallback;
import com.wshl.aijiuunion.controller.activity.JIamengShebeiActivity;
import com.wshl.aijiuunion.controller.activity.SetActivity;
import com.wshl.aijiuunion.controller.activity.VideoDetailActivity;
import com.wshl.aijiuunion.controller.activity.WebActivity;
import com.wshl.aijiuunion.utils.CountDownHandler;
import com.wshl.aijiuunion.utils.GlideUtils;
import com.wshl.aijiuunion.utils.share.LoginOutUtils;
import com.wshl.aijiuunion.view.bean.JIamenSheiBeiBean;
import com.wshl.aijiuunion.view.bean.PwdBean;
import com.wshl.aijiuunion.view.bean.ShenqingJIamengBean;
import com.wshl.aijiuunion.view.widget.Loadingdialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created buy 风凊扬 on 2019/11/7 0007
 */
public class RequestModel {

    public void videoCate(final Activity mContext, final RequestCallback callback) {
        if (!NetworkUtils.isConnected()) {
            callback.onIntentError();
            return;
        }

        OkGo.<String>post(NetUrl.VIDEOCATE)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseJson(mContext, response, callback, "视频分类:");
                    }
                });

    }

    public void videoRecord(final Activity mContext, int page, final RequestCallback callback) {

        if (!NetworkUtils.isConnected()) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.VIDEORECORD)
                .tag(mContext)
                .params("token", token)
                .params("page", page)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseJson(mContext, response, callback, "观看记录:");

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        callback.onError(response.code());
                        LogUtils.e("观看记录报错:" + response.code());
                    }
                });

    }

    public void videoDetail(final Activity mContext, int id, final RequestCallback callback) {

        if (!NetworkUtils.isConnected()) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.VIDEODETAIL)
                .tag(mContext)
                .params("token", token)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseJson(mContext, response, callback, "视频详情:");

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        callback.onError(response.code());
                    }
                });


    }

    public void videoList(final Activity mContext, int type, int cateid, int page, final RequestCallback callback) {

        if (!NetworkUtils.isConnected()) {
            callback.onIntentError();
            return;
        }

        OkGo.<String>post(NetUrl.VIDEOLIST)
                .tag(mContext)
                .params("type", type)
                .params("cate_id", cateid)
                .params("page", page)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseJson(mContext, response, callback, "视频列表:");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });

    }

    public void videoIndex(final Activity mContext, final RequestCallback callback) {
        if (!NetworkUtils.isConnected()) {
            callback.onIntentError();
            return;
        }
        OkGo.<String>post(NetUrl.VIDEOINDEX)
                .tag(mContext)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseJson(mContext, response, callback, "视频首页:");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });


    }

    public void isShowVideo(final Activity mContext, final RequestCallback callback) {
        if (!NetworkUtils.isConnected()) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.ISSHOWVIDEO)
                .tag(mContext)
                .params(GlobConstant.TOKEN, token)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseJson(mContext, response, callback, "是否显示视频:");
                    }
                });

    }


    /**
     * 首页数据
     *
     * @param mContext
     */
    public void getHomeData(Context mContext, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }


        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        String lat = SPUtils.getInstance().getString(GlobConstant.LATITUDE);
        String lng = SPUtils.getInstance().getString(GlobConstant.LNGTITUDE);

        OkGo.<String>post(NetUrl.INDEX)
                .params(GlobConstant.TOKEN, token)
                .params("lat", lat)
                .params("lng", lng)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Loadingdialog.dismiss();
                        LogUtils.i("首页数据:" + response.body());
                        callback.onSuccess(response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });
    }


    public void clearHistory(final Activity mContext, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.SEARCHDEL)
                .tag(mContext)
                .params(GlobConstant.TOKEN, token)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseJson(mContext, response, callback, "删除历史记录:");
                    }
                });


    }

    /**
     * 商城首页数据
     *
     * @param mContext
     * @param callback
     */
    public void getShopData(Context mContext, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.MALL)
                .params(GlobConstant.TOKEN, token)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Loadingdialog.dismiss();
                        LogUtils.i("商城首页数据:" + response.body());
                        callback.onSuccess(response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });


    }


    /**
     * 上传图片
     *
     * @param mContext
     * @param imgpath
     * @param callback
     */
    public void uploadImg(final Activity mContext, String imgpath, final RequestCallback callback) {
        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);

        OkGo.<String>post(NetUrl.UPLOAD)
                .tag(this)
                .params("token", token)
                .params("file", new File(imgpath))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Loadingdialog.dismiss();
                        LogUtils.i("上传图片:" + response.body());


                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            if (jsonObject.has("code")) {
                                String code = jsonObject.getString("code");
                                switch (code) {
                                    case GlobConstant.REQUESTSUCCESS:
                                        if (jsonObject.has("data")) {

                                            String data = jsonObject.getString("data");
                                            JSONObject datajson = new JSONObject(data);
                                            if (datajson.has("url")) {

                                                callback.onSuccess(datajson.getString("url"));
                                            }

                                        }
                                        break;
                                    case GlobConstant.REQUESTERROR:
                                        LoginOutUtils.reLogin(mContext);
                                        break;
                                    default:

                                        break;
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });

    }

    /**
     * 修改用户信息
     *
     * @param avatar   头像
     * @param nickname 昵称
     */
    public void userEdit(Context mContext, final String avatar, final String nickname, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        PostRequest<String> post = OkGo.<String>post(NetUrl.USEREDIT);
        post.tag(this)
                .params("token", token);
        if (!TextUtils.isEmpty(avatar)) {
            post.params("key", "avatar");
            post.params("value", avatar);
        }

        if (!TextUtils.isEmpty(nickname)) {
            post.params("key", "name");
            post.params("value", nickname);
        }
        post.execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Loadingdialog.dismiss();
                LogUtils.i("修改用户信息:" + response.body());
                callback.onSuccess(response.body());
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                Loadingdialog.dismiss();
                callback.onError(response.code());
            }


        });

    }

    /**
     * 充值选项列表
     *
     * @param mContext
     * @param callback
     */
    public void getRechargeList(Context mContext, final RequestCallback callback) {
        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.RECHARGELIST)

                .tag(this)
                .params(GlobConstant.TOKEN, token)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Loadingdialog.dismiss();
                        callback.onSuccess(response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });
    }


    /**
     * 修改密码
     *
     * @param mContext
     * @param pwdBean
     * @param callback
     */
    public void editPwd(Context mContext, PwdBean pwdBean, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        String code = pwdBean.getCode();
        if (TextUtils.isEmpty(code)) {
            Loadingdialog.dismiss();
            ToastUtils.showShort("请输入验证码");
            return;
        }
        String newpwd = pwdBean.getNewpwd();
        if (TextUtils.isEmpty(newpwd)) {
            Loadingdialog.dismiss();
            ToastUtils.showShort("请输入密码");
            return;
        }

        String confirmpwd = pwdBean.getConfirmpwd();
        if (TextUtils.isEmpty(confirmpwd)) {
            Loadingdialog.dismiss();
            ToastUtils.showShort("请再次输入密码");
            return;
        }

        if (!TextUtils.equals(newpwd, confirmpwd)) {
            Loadingdialog.dismiss();
            ToastUtils.showShort("两次输入的密码不一致");
            return;
        }
        OkGo.<String>post(NetUrl.CHANGEPWD)

                .tag(this)
                .params(GlobConstant.TOKEN, token)
                .params("phone", pwdBean.getMobile())
                .params("code", code)
                .params("payword", newpwd)
                .params("repayword", confirmpwd)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        Loadingdialog.dismiss();
                        callback.onSuccess(response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });
    }


    /**
     * 获取个人信息
     *
     * @param mContext
     * @param callback
     */
    public void getUserinfo(final Activity mContext, final RequestCallback callback) {
        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);

        OkGo.<String>post(NetUrl.USERINFO)
                .tag(mContext)
                .params(GlobConstant.TOKEN, token)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseJson(mContext, response, callback, "个人信息:");

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                        LogUtils.e("获取个人信息失败:" + response.code());
                    }
                });

    }


    /**
     * 版本更新
     *
     * @param mContext
     * @param version
     * @param callback
     */
    public void checkVersion(final Activity mContext, String version, final boolean istishi, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }

        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.CHECKVERSION)
                .tag(this)
                .params(GlobConstant.TOKEN, token)
                .params("version", version)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        //parseJson(mContext, response, callback, "版本更新:");

                        Loadingdialog.dismiss();
                        LogUtils.i("版本更新:" + response.body());
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            if (jsonObject.has("code")) {

                                String code = jsonObject.getString("code");
                                switch (code) {
                                    case GlobConstant.REQUESTSUCCESS:

                                        callback.onSuccess(response.body());
                                        break;
                                    case GlobConstant.REQUESTERROR://token过期
                                        LoginOutUtils.reLogin(mContext);
                                        break;
                                    default:
                                        if (istishi) {
                                            if (jsonObject.has("msg")) {

                                                ToastUtils.showLong(jsonObject.getString("msg"));
                                            }
                                        }


                                        break;
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });

    }


    /**
     * 充值
     *
     * @param mContext
     * @param rechargeid
     * @param payment
     * @param callback
     */
    public void recharge(final Activity mContext, final int rechargeid, String payment, final RequestCallback callback) {

        if (rechargeid == 0) {
            ToastUtils.showShort("请选择充值选项");
            return;
        }
        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.RECHARGE)
                .tag(mContext)
                .params(GlobConstant.TOKEN, token)
                .params("id", rechargeid)
                .params("payment", payment)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseJson(mContext, response, callback, "充值");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });

    }


    /**
     * 店铺列表
     *
     * @param mContext
     * @param lat
     * @param lng
     * @param callback
     */
    public void storeList(final Activity mContext, int page, String lat, String lng, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.STORELIST)
                .tag(mContext)
                .params(GlobConstant.TOKEN, token)
                .params("lat", lat)
                .params("lng", lng)
                .params("page", page)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseJson(mContext, response, callback, "附近店铺列表:");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });


    }


    /**
     * 搜索
     *
     * @param mContext
     * @param callback
     */
    public void search(final Activity mContext, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);

        OkGo.<String>post(NetUrl.SEARCH)
                .tag(mContext)
                .params(GlobConstant.TOKEN, token)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseJson(mContext, response, callback, "搜索:");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });


    }


    /**
     * 加盟设备
     *
     * @param mContext
     * @param bean
     * @param callback
     */
    public void jiaMengShebei(final Activity mContext, JIamenSheiBeiBean bean, final RequestCallback callback) {

        if (!NetworkUtils.isConnected()) {
            ToastUtils.showLong(mContext.getString(R.string.neterror));
            callback.onIntentError();
            return;
        }

        String trurname = bean.getTrurname();

        if (TextUtils.isEmpty(trurname)) {
            ToastUtils.showShort("请输入真实姓名");
            return;
        }
        String code = bean.getCode();

        if (TextUtils.isEmpty(code)) {
            ToastUtils.showShort("请输入验证码");
            return;
        }
        String tuijianma = bean.getTuijianma();

        String company = bean.getCompany();
        if (TextUtils.isEmpty(company)) {

            ToastUtils.showShort("请输入公司名称");
            return;
        }
        String mobile = bean.getMobile();

        String yingyizhizhaourl = bean.getYingyizhizhaourl();
        if (TextUtils.isEmpty(yingyizhizhaourl)) {
            ToastUtils.showShort("请上传营业执照");
            return;
        }
        String storeurl = bean.getStoreurl();
        if (TextUtils.isEmpty(storeurl)) {
            ToastUtils.showShort("请上传店铺照片");
            return;
        }
        String provinceid = bean.getProvinceid();
        String cityid = bean.getCityid();
        String areaid = bean.getAreaid();

        if (TextUtils.isEmpty(provinceid) || TextUtils.isEmpty(cityid) || TextUtils.isEmpty(areaid)) {
            ToastUtils.showShort("请选择区域");
            return;
        }

        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);

        Loadingdialog.showAlert(mContext);

        OkGo.<String>post(NetUrl.JIAMENGSHEBEI)
                .tag(mContext)
                .params("token", token)
                .params("truename", trurname)
                .params("phone", mobile)
                .params("code", code)
                .params("company", company)
                .params("referee_code", tuijianma)
                .params("cover", storeurl)
                .params("image", yingyizhizhaourl)
                .params("province_id", provinceid)
                .params("city_id", cityid)
                .params("area_id", areaid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseJson(mContext, response, callback, "加盟设备:");

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }
                });


    }

    /**
     * 用户协议
     *
     * @param mContext
     * @param callback
     */
    public void xieYi(final Activity mContext, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }

        OkGo.<String>post(NetUrl.USERAGREE)
                .tag(mContext)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseJson(mContext, response, callback, "用户协议:");

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }

                });


    }


    public void AUTHGROUP(final Activity mContext, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }

        String recommendcode = SPUtils.getInstance().getString(GlobConstant.RECOMMENDCODE);

        OkGo.<String>post(NetUrl.AUTHGROUP)
                .tag(mContext)
                .params("referee_code", recommendcode)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseJson(mContext, response, callback, "获取申请加盟可申请身份:");

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        callback.onError(response.code());
                    }

                });


    }

    public void GROUPNUM(final Activity mContext, final String groupid, final RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }

        OkGo.<String>post(NetUrl.GROUPNUM)
                .tag(mContext)
                .params("groupid", groupid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseJson(mContext, response, callback, "获取艾灸条数目:");

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        LogUtils.e("获取艾灸条数目:" + response.code());
                        callback.onError(response.code());
                    }

                });


    }

    /**
     * 申请加盟
     *
     * @param mContext
     * @param bean
     * @param callback
     */
    public void jiameng(final Activity mContext, ShenqingJIamengBean bean, final RequestCallback callback) {
        if (!NetworkUtils.isConnected()) {
            ToastUtils.showLong(mContext.getString(R.string.neterror));
            callback.onIntentError();
            return;
        }

        String name = bean.getName();
        String mobile = bean.getMobile();
        String shenfencode = bean.getShenfencode();
        String shenfengzheng = bean.getShenfengzheng();
        String shenfenfan = bean.getShenfenfan();
        String type = bean.getType();//1 分公司 2代理 3业务员
        String yingyezhizhaoimg = bean.getYingyezhizhaoimg();
        //  String yingyezhizhaoname = bean.getYingyezhizhaoname();
        List<String> imglist = bean.getImglist();
        String companyname = bean.getCompanyname();
        String detailaddress = bean.getDetailaddress();
        String provinceid = bean.getProvinceid();
        String cityid = bean.getCityid();
        String areaid = bean.getAreaid();

        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort("请输入真实姓名");
            return;
        }

        if (TextUtils.isEmpty(mobile)) {
            ToastUtils.showShort("请输入手机号");
            return;
        }

        if (TextUtils.isEmpty(shenfencode)) {
            ToastUtils.showShort("请输入身份证号");
            return;
        }

        if (TextUtils.isEmpty(shenfengzheng)) {
            ToastUtils.showShort("请上传身份证正面");
            return;
        }

        if (TextUtils.isEmpty(shenfenfan)) {
            ToastUtils.showShort("请上传身份证反面");
            return;
        }

        LogUtils.i("type:" + type);
        if (!type.equals("3")) {

//            if (TextUtils.isEmpty(yingyezhizhaoname)) {
//                ToastUtils.showShort("请输入营业执照名称");
//                return;
//            }

            if (TextUtils.isEmpty(yingyezhizhaoimg)) {
                ToastUtils.showShort("请上传营业执照");
                return;
            }

            if (TextUtils.isEmpty(companyname)) {
                ToastUtils.showShort("请输入公司名称");
                return;
            }

            if (imglist.size() == 0) {

                ToastUtils.showShort("请上传公司图片");
                return;
            }
        }


        if (TextUtils.isEmpty(provinceid) || TextUtils.isEmpty(cityid) || TextUtils.isEmpty(areaid)) {
            ToastUtils.showShort("请选择区域");
            return;
        }

        if (TextUtils.isEmpty(detailaddress)) {

            ToastUtils.showShort("请输入详细地址");
            return;
        }


        Loadingdialog.showAlert(mContext);
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        //   String groupid = SPUtils.getInstance().getString(GlobConstant.TYPE);
        LogUtils.i("groupid:" + type);
        PostRequest<String> post = OkGo.<String>post(NetUrl.APPLY);
        post.tag(mContext)
                .params("token", token)
                .params("groupid", type)
                .params("truename", name)
                .params("phone", mobile)
                .params("cardno", shenfencode)
                .params("cover1", shenfengzheng)
                .params("cover2", shenfenfan)
                .params("province_id", provinceid)
                .params("city_id", cityid)
                .params("area_id", areaid)
                .params("address", detailaddress);

        LogUtils.i("type:" + type);
        if (!type.equals("3")) {
            post.params("name", companyname);
            post.params("cover", yingyezhizhaoimg);
            for (int i = 0; i < imglist.size(); i++) {
                post.params("images[" + i + "]", imglist.get(i));
            }
        }


        post.execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Loadingdialog.dismiss();
                parseJson(mContext, response, callback, "申请加盟:");
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                Loadingdialog.dismiss();
                callback.onError(response.code());
            }

        });

    }

    /**
     * 唤起微信支付界面
     */
    public void payWechat(Context context, String appid, String partnerid, String prepayid, String noncestr, String timestamp, String packagevalue, String sign) {
        PayReq req = new PayReq();
        req.appId = appid;
        req.partnerId = partnerid;
        req.prepayId = prepayid;
        req.nonceStr = noncestr;
        req.timeStamp = timestamp;
        req.packageValue = packagevalue;
        req.sign = sign;
        IWXAPI api = WXAPIFactory.createWXAPI(context, appid);
        api.sendReq(req);
    }


    private void parseJson(Activity mContext, Response<String> response, RequestCallback callback, String s) {
        Loadingdialog.dismiss();
        LogUtils.i(s + response.body());
        try {
            JSONObject jsonObject = new JSONObject(response.body());
            if (jsonObject.has("code")) {

                String code = jsonObject.getString("code");
                switch (code) {
                    case GlobConstant.REQUESTSUCCESS:

                        callback.onSuccess(response.body());
                        break;
                    case GlobConstant.REQUESTERROR://token过期
                        LoginOutUtils.reLogin(mContext);
                        break;
                    default:
                        if (jsonObject.has("msg")) {

                            ToastUtils.showLong(jsonObject.getString("msg"));
                        }

                        break;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 网络是否连接
     *
     * @param mContext
     */
    private boolean isIntentConnect(Context mContext) {

        if (!NetworkUtils.isConnected()) {
            ToastUtils.showLong(mContext.getString(R.string.neterror));
            return false;
        }

        Loadingdialog.showAlert(mContext);

        return true;
    }

    /**
     * 发送验证码
     *
     * @param mContext
     * @param mobile
     */
    public void sendCode(Context mContext, final String mobile) {
        if (!NetworkUtils.isConnected()) {
            ToastUtils.showShort(mContext.getString(R.string.neterror));
            return;
        }

        OkGo.<String>post(NetUrl.SENDMSG)
                .tag(this)
                .params("phone", mobile)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Loadingdialog.dismiss();
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            if (jsonObject.has("code")) {

                                String code = jsonObject.getString("code");
                                switch (code) {
                                    case GlobConstant.REQUESTERROR:
                                        if (jsonObject.has("msg")) {
                                            ToastUtils.showShort(jsonObject.getString("msg"));
                                        }
                                        break;
                                    case GlobConstant.REQUESTSUCCESS:
                                        //开始倒计时
                                        mCountDownHandler.sendMessage(mCountDownHandler.obtainMessage(COUNT_DOWN_START));
                                        ToastUtils.showShort("验证码已发送到 " + mobile);
                                        break;
                                    default:
                                        if (jsonObject.has("msg")) {
                                            ToastUtils.showShort(jsonObject.getString("msg"));
                                        }
                                        break;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Loadingdialog.dismiss();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Loadingdialog.dismiss();
                        ToastUtils.showShort("验证码发送失败,错误码:" + response.code());
                    }
                });
    }

    public void zhuxiao(final Activity mContext, final RequestCallback callback) {
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.XIAOHU)
                .tag(mContext)
                .params(GlobConstant.TOKEN, token)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseJson(mContext, response, callback, "注销:");
                    }
                });
    }

    public void addComment(final Activity mContext, int videoid, String content, RequestCallback callback) {

        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.VIDEOCOMMENT)
                .params("id", videoid)
                .params("token", token)
                .params("content", content)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseJson(mContext, response, callback, "添加评论:");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        callback.onError(response.code());
                        Loadingdialog.dismiss();
                    }
                });
    }

    public void getCommentlist(VideoDetailActivity mContext, int videoid, int page, RequestCallback callback) {


        if (!isIntentConnect(mContext)) {
            callback.onIntentError();
            return;
        }
        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        OkGo.<String>post(NetUrl.VIDEOCOMMENTS)
                .params("id", videoid)
                .params("token", token)
                .params("page", page)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseJson(mContext, response, callback, "评论列表:");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        callback.onError(response.code());
                        Loadingdialog.dismiss();
                    }
                });
    }


    public interface onSendcodeListener {


        void start(int s);

        void end();
    }


    public onSendcodeListener sendcodeListener;

    public void setOnsendCodeListener(onSendcodeListener onSendcodeListener) {

        sendcodeListener = onSendcodeListener;

    }

    protected static final int COUNT_DOWN_SECONDS = 60;
    protected static final int COUNT_DOWN_START = 0;
    /**
     * 倒计时handler
     */
    private CountDownHandler mCountDownHandler = new CountDownHandler(
            COUNT_DOWN_SECONDS, 1000) {

        @Override
        protected void onTick(int surplus) {
            if (sendcodeListener != null) {
                sendcodeListener.start(surplus);
            }
        }

        @Override
        protected void onFinished() {
            if (sendcodeListener != null) {
                sendcodeListener.end();
            }
        }
    };
}
